package view;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

/*
 * Funciona assim
 * DialogoLogin d = new DialogoLogin(this);
		System.out.println(d.getLogin() + " " + d.getSenha());
		d.dispose();
 */
public class DialogoLogin extends javax.swing.JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField campoLogin;
	private JLabel labelLogin;
	private JButton botaoAutentica;
	private JPasswordField campoSenha;
	private JanelaDoCliente janela;
	private JLabel labelSenha;
	private String login;
	private String senha;

	/**
	* Auto-generated main method to display this JDialog
	*/
	
	public DialogoLogin(JanelaDoCliente janela) {
		super(janela);
		initGUI();
		this.janela = janela;
		this.setModal(true);
		this.setLocationRelativeTo(null); //centralizar
		this.setVisible(true);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
	}
	
	private void initGUI() {
		try {
			{
				this.setTitle("Autenticação");
				GridLayout thisLayout = new GridLayout(3, 2);
				thisLayout.setHgap(5);
				thisLayout.setVgap(5);
				thisLayout.setColumns(1);
				getContentPane().setLayout(thisLayout);
				{
					labelLogin = new JLabel();
					getContentPane().add(labelLogin);
					labelLogin.setText("Login");
					labelLogin.setBounds(23, 12, 33, 15);
				}
				{
					campoLogin = new JTextField();
					getContentPane().add(campoLogin);
					campoLogin.setBounds(74, 12, 142, 22);
				}
				{
					labelSenha = new JLabel();
					getContentPane().add(labelSenha);
					labelSenha.setText("Senha");
					labelSenha.setBounds(12, 42, 40, 15);
				}
				{
					campoSenha = new JPasswordField();
					getContentPane().add(campoSenha);
					campoSenha.setBounds(74, 39, 142, 22);
				}
				{
					botaoAutentica = new JButton();
					getContentPane().add(botaoAutentica);
					botaoAutentica.setText("Autenticar");
					botaoAutentica.setBounds(74, 67, 79, 22);
					botaoAutentica.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							botaoAutenticaActionPerformed(evt);
						}
					});
				}
			}
			
			this.addWindowListener(new WindowAdapter() {
				
				@Override
				public void windowClosing(WindowEvent e) {
					// TODO Auto-generated method stub
					super.windowClosing(e);
					if(verificaRetornoDaAutenticacao()){
						DialogoLogin.this.setModal(false);
						DialogoLogin.this.dispose();
					}
					else{
						janela.setVisible(false);
						DialogoLogin.this.setModal(false);
						DialogoLogin.this.dispose();
						System.exit(0);
					}
				}
				
			});
			
			this.pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void botaoAutenticaActionPerformed(ActionEvent evt) {
		//System.out.println("botaoAutentica.actionPerformed, event="+evt);
		//TODO add your code for botaoAutentica.actionPerformed
		login = campoLogin.getText();
		senha = String.copyValueOf(campoSenha.getPassword());
		if (senha == null)
			senha = "";
		//solicita autenticação
		try {
			janela.getCliente().solicitaAutenticacao(login, senha);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Thread.sleep(7777); //Aguarda um tempo até receber todos os dados
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (verificaRetornoDaAutenticacao()){
			this.setModal(false);
			this.dispose();			
		}	
		else
			JOptionPane.showConfirmDialog(this, "Usuario ou senha invalidos!", 
					"Login", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
	}
	
	private boolean verificaRetornoDaAutenticacao(){
		//se tiver online é porque foi aprovada a autenticação;
		return janela.getCliente().isEstado();
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getLogin() {
		return login;
	}


}
